import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'
import {
  UPDATE_ACCOUNT_TEAM_QUERY_DATA,
  UPDATE_FOLLOW_RECORD_ANALYZE_QUERY_DATA,
  UPDATE_WEEK_FOLLOW_RECORD_ANALYZE_QUERY_DATA,
  UPDATE_ANALYZE_QUERY_DATA,
  UPDATE_YU_YUE_QUERY_DATA,
  UPDATE_CLUE_TYPE_COUNT_QUERY_DATA,
  UPDATE_CUSTOM_ORDER_ANALYZE_QUERY_DATA
} from '../mutation-types'
var dateFormat = require('dateformat')
export default {
  saveQueryData ({ commit }, data) {
    commit(UPDATE_ACCOUNT_TEAM_QUERY_DATA, data)
  },

  followRecordAnalyzeQueryData ({ commit }, data) {
    // console.log('followRecordAnalyzeQueryData', JSON.stringify(data))
    commit(UPDATE_FOLLOW_RECORD_ANALYZE_QUERY_DATA, Object.assign({}, data))
  },
  weekFollowRecordAnalyzeQueryData ({ commit }, data) {
    // console.log('followRecordAnalyzeQueryData', JSON.stringify(data))
    commit(UPDATE_WEEK_FOLLOW_RECORD_ANALYZE_QUERY_DATA, Object.assign({}, data))
  },
  analyzeQueryData ({ commit }, data) {
    commit(UPDATE_ANALYZE_QUERY_DATA, Object.assign({}, data))
  },
  yuYueQueryData ({ commit }, data) {
    commit(UPDATE_YU_YUE_QUERY_DATA, Object.assign({}, data))
  },
  clueTypeCountQueryData ({ commit }, data) {
    commit(UPDATE_CLUE_TYPE_COUNT_QUERY_DATA,Object.assign({}, data))
  },
  customOrderAnalyzeQueryData ({ commit }, data) {
    commit(UPDATE_CUSTOM_ORDER_ANALYZE_QUERY_DATA,Object.assign({}, data))
  },

  getTeams ({ dispatch, commit, state, rootState }) {
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/teams'
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getExcludeSelfTeams ({ dispatch, commit, state, rootState }) {
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/sites/' + rootState.auth.user.site_id + '/get/exclude_user/teams'
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getGuWen ({ dispatch, commit, state, rootState }) {
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/admin/' + rootState.auth.user.token + '/assists'
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getYuYueList ({ rootState }, info) {
    const frag = '?' + U(info) + '&site_id=' + rootState.auth.user.site_id
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/yu_yue' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getFollowRecordAnalyze ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/follow_record_analyze' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getCustomBargainAnalyze ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/custom_bargain_analyze' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getClueTypeCount ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/get_clue_type_count' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getNewCustom ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/get_new_custom' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getSuccessCustom ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/get_success_custom' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getCustomOrderAnalyze ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/get_custom_order_analyze' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  getCustomOrderList ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/get_custom_order_list' + frag
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  }
}
